﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
using System.Web;
using EncryptUtily;

using ValidateCommon;

namespace RichWebServiceDll
{
    public class RichWSLogin
    { 
        
        /// <summary>
        /// 服务器返回信息
        /// </summary>
        private string _requestResultMessage;
        public string RequestResultMessage
        {
            get { return _requestResultMessage; }
            set { _requestResultMessage = value; }
        }

        public RichWSLogin(string strServerIp, string strName, string strPsd)
        {
            try
            {
                StringBuilder builderWebServiceAddress = new StringBuilder();

                builderWebServiceAddress.Append("http://" + strServerIp + "/IWebServices/LoginWebSite.asmx/CsLoginValidate");
                builderWebServiceAddress.Append("?strWebUrl=" + strServerIp);
                builderWebServiceAddress.Append("&strUserName=" + UTF8code.RepleaseDES(EncryptUtily.EncryptUtily.LoginEncrypt(strName)));
                builderWebServiceAddress.Append("&strPsw=" + UTF8code.RepleaseDES(EncryptUtily.EncryptUtily.LoginEncrypt(strPsd)));
                WebRequest webrequest = WebRequest.Create(builderWebServiceAddress.ToString());
                WebResponse webresponse = webrequest.GetResponse();
                if (webresponse != null)
                {
                    var stream = webresponse.GetResponseStream();
                    if (stream != null)
                    {
                        var sr = new StreamReader(stream, Encoding.UTF8);
                        //读取返回流
                        XmlDocument xd = new XmlDocument();
                        xd.Load(sr);
                        string strResponseString = xd.ChildNodes[1].InnerText.ToString();
                        Hashtable resultHashtable = AnalysisWebResponse(strResponseString);
                        this._requestResultMessage = resultHashtable["RequestReturnMessage"].ToString();
                    }
                    else
                    {
                        this._requestResultMessage = "未获取返回信息";
                    }
                }
                else
                {
                    this._requestResultMessage = "提交数据失败;服务器未响应";
                }
            }
            catch (Exception ex)
            {
                throw new Exception("提交服务器数据时异常:" + ex.Message.Replace("\r\n", ""));
            }
        }
        /// <summary>
        /// 处理返回字符数据并进行分割
        /// </summary>
        /// <param name="strWebResponse">webservice响应</param>
        /// <returns>分割后的哈希值对</returns>
        private static Hashtable AnalysisWebResponse(string strWebResponse)
        {
            string[] responseResult = strWebResponse.Split('_');
            Hashtable returnResult = new Hashtable();
            returnResult["RequestReturnMessage"] = responseResult[0];

            return returnResult;
        }
    }
}
